package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.TransferClass;
import cn.wolfcode.crm.mapper.FormalStudentMapper;
import cn.wolfcode.crm.mapper.TransferClassMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ITransferClassService;
import cn.wolfcode.crm.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class TransferClassServiceImpl implements ITransferClassService {
    @Autowired
    private TransferClassMapper transferClassMapper;
    @Autowired
    private FormalStudentMapper formalStudentMapper;

    public int insert(TransferClass record) {

        return transferClassMapper.insert(record);
    }

    public int updateByPrimaryKey(TransferClass record) {

        return transferClassMapper.updateByPrimaryKey(record);
    }


    public List<TransferClass> selectAll() {

        return transferClassMapper.selectAll();
    }

    public PageResult query(QueryObject qo) {
        int total = transferClassMapper.query4Count(qo);
        if(total > 0){
            return new PageResult(total,transferClassMapper.query4List(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    public int deleteByPrimaryKey(Long id) {
        TransferClass transferClass = transferClassMapper.selectByPrimaryKey(id);
        Long formalStudentId = transferClass.getFormalStudentId();
        formalStudentMapper.updateStatus(formalStudentId);
        return transferClassMapper.deleteByPrimaryKey(id);
    }

    public void audit(Long transferClassId) {
        TransferClass transferClass = transferClassMapper.selectByPrimaryKey(transferClassId);
        formalStudentMapper.transfer(transferClass.getWantClass().getId(),transferClass.getFormalStudentId());
        transferClassMapper.audit(transferClassId);
    }
}
